package com.pi.pro.xcmg.am.rf.web.cust.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.bean.PageResult;
import com.pi.framework.core.bean.Result;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.pro.xcmg.am.common.vo.act.CounWorkHeadDto;
import com.pi.pro.xcmg.am.common.vo.act.CounWorkLineDto;
import com.pi.pro.xcmg.am.common.vo.act.WaveShipHeadDto;
import com.pi.pro.xcmg.am.common.vo.act.WaveShipLineDto;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.CountTaskHeaderDto;
import com.pi.wms.dto.act.CountTaskLineDto;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.dto.base.LocationLineDto;
import com.pi.wms.dto.outbound.SoStorageShipMatchDto;
import com.pi.wms.entity.*;
import com.pi.wms.fac.LocationResolver;
import com.pi.wms.rf.api.controller.BaseApiController;
import com.pi.wms.service.AllocationService;
import com.pi.wms.service.MaterialService;
import com.pi.wms.service.TaskService;
import com.pi.wms.service.WaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Api(value = "盘点管理")
@RestController
@RequestMapping("/inventory/count/ca")
public class CustApplyCountApiController extends BaseApiController<TwtActTaskList, Long> {

	@Autowired
	private TaskService service;

	@Autowired
	private MaterialService materialService;
	@Autowired
	private LocationResolver locationResolver;

	@Autowired
	private WasCustomWorkService wasCustomWorkService;

	@Autowired
	private WcsCustomWorkService wcsCustomWorkService;

	//
	private Cache<Long, String> locationZoneCache= CacheBuilder.newBuilder().expireAfterWrite(20, TimeUnit.MINUTES).build();


	@Override
	protected ICrudService<TwtActTaskList, Long> getCrudService() {
		return this.service;
	}
	
	@JsonView(value = { PageResult.View.class })
	@ApiOperation("查询指定托盘的盘点明细")
	@GetMapping("/query")
	public Result query(
            @ApiParam(name = "containnumber") @RequestParam(name = "containnumber", required=true) String containnumber,
            @ApiParam(name = "documentnumber") @RequestParam(name = "documentnumber", required=false) String documentnumber,
            @ApiParam(name = "taskid") @RequestParam(name = "taskid", required=false) String taskid,
            @ApiParam(name = "workmode") @RequestParam(name = "workmode", required=false) String workmode,
            @ApiParam(name = "warehousecode") @RequestParam(name = "warehousecode", required=false) String warehousecode,
			@ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {
		
		if ((StringUtils.isNullOrBlank(containnumber))
                && (StringUtils.isNullOrBlank(documentnumber)) ){
			return resultError(501, "请扫描托盘码");
		}
        containnumber=containnumber.trim();
		if (documentnumber!=null){
			documentnumber=documentnumber.trim();
			if ((documentnumber.indexOf("x")>=0) || (documentnumber.indexOf("X")>=0)){
				documentnumber=documentnumber.replace("x","%");
				documentnumber=documentnumber.replace("X","%");
			}
		}
        Long taskidValue=null;
        if (!StringUtils.isNullOrBlank(taskid)){
			taskidValue=StringUtils.getLongValue(taskid);
		}
        Map<String, Object> map = new HashMap<String, Object>();
        CounWorkHeadDto header=new CounWorkHeadDto();
		List<TwtActTaskList> tasks=this.service.queryWorkingTasks(WmsConstants.TASK_TYPE_COUNT, containnumber);
		if ((tasks==null) || (tasks.size()==0)){
		    header.setReturestate("E");
		    header.setReturetext("找不到盘点数据.托盘码:"+containnumber);
            map.put("header", header);
			return resultError(501, "找不到盘点记录.");
		}

		List<CounWorkLineDto> lines=new ArrayList<CounWorkLineDto>();
		this.wasCustomWorkService.populate(header,lines,tasks);

        header.setReturestate("S");
        header.setReturetext("");
		map.put("header", header);
		map.put("lines", lines);
		return resultOk("读取盘点数据成功", Arrays.asList(map));
	}


	@JsonView(value = { PageResult.View.class })
	@ApiOperation("确认盘点")
	@GetMapping("/confirm")
	public Result confirm(@ApiParam(name = "taskid") @RequestParam(name = "taskid", required=true) Long taskid,
                          @ApiParam(name = "quantity") @RequestParam(name = "quantity", required=false) BigDecimal quantity,
                          @ApiParam(name = "countquantity") @RequestParam(name = "countquantity", required=false) BigDecimal countquantity,
                       @ApiParam(name = "containnumber") @RequestParam(name = "containnumber", required=false) String containnumber,
                       @ApiParam(name = "materialcode") @RequestParam(name = "checkmaterialcode", required=false) String materialcode,
                       @ApiParam(name = "documentnumber") @RequestParam(name = "documentnumber", required=false) String documentnumber,
                       @ApiParam(name = "worktype") @RequestParam(name = "worktype", required=false) String worktype,
                       @ApiParam(name = "workmode") @RequestParam(name = "workmode", required=false) String workmode,
					   @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

	    if (taskid==null){
            return resultError(501, "盘点失败,请扫描托盘获取任务.");
        }

        //确认盘点
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setWorkMode(workmode);
        actionDto.setWorker(SecurityUtils.getLoginUsername());
		String m = this.wasCustomWorkService.confirmCount(taskid, quantity, countquantity, actionDto);
	    if (!StringUtils.isNullOrBlank(m)){
            return resultError(501, "盘点失败:"+m);
        }

		return resultOk("发运成功");
	}


}
